{#
    SPDX-FileCopyrightText: NVIDIA CORPORATION & AFFILIATES
    Copyright (c) 2024-2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
    Apache-2.0
    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at
    http://www.apache.org/licenses/LICENSE-2.0
    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
#}

{% import 'traffic_config.j2' as traffic_config %}

{%- macro generate_dscp_to_tc_map_per_sku() -%}
    "DSCP_TO_TC_MAP": {
        "AZURE": {
            "0" : "0",
            "1" : "1",
            "2" : "1",
            "3" : "2",
            "4" : "2",
            "5" : "3",
            "6" : "3",
            "7" : "0",
            "8" : "0",
            "9" : "0",
            "10": "0",
            "11": "4",
            "12": "4",
            "13": "4",
            "14": "4",
            "15": "4",
            "16": "4",
            "17": "4",
            "18": "4",
            "19": "4",
            "20": "4",
            "21": "4",
            "22": "4",
            "23": "4",
            "24": "4",
            "25": "4",
            "26": "4",
            "27": "4",
            "28": "4",
            "29": "4",
            "30": "4",
            "31": "4",
            "32": "4",
            "33": "4",
            "34": "4",
            "35": "4",
            "36": "4",
            "37": "4",
            "38": "4",
            "39": "4",
            "40": "4",
            "41": "5",
            "42": "5",
            "43": "5",
            "44": "5",
            "45": "5",
            "46": "5",
            "47": "5",
            "48": "5",
            "49": "5",
            "50": "5",
            "51": "0",
            "52": "0",
            "53": "0",
            "54": "0",
            "55": "0",
            "56": "0",
            "57": "0",
            "58": "0",
            "59": "0",
            "60": "0",
            "61": "0",
            "62": "0",
            "63": "0"
        }
    },
{%- endmacro -%}
{%- macro generate_tc_to_pg_map_per_sku() -%}
    "TC_TO_PRIORITY_GROUP_MAP": {
        "AZURE": {
            "0": "0",
            "1": "0",
            "2": "0",
            "3": "0",
            "4": "0",
            "5": "0",
            "6": "0",
            "7": "0"
        }
    },
{%- endmacro -%}

{%- macro generate_scheduler_per_sku() -%}
    "SCHEDULER": {
        "scheduler_q0_downlink": {
            "type": "DWRR",
            "weight": "4"
        },
        "scheduler_q1_downlink": {
            "type": "DWRR",
            "weight": "8"
        },
        "scheduler_q2_downlink": {
            "type": "DWRR",
            "weight": "18"
        },
        "scheduler_q3_downlink": {
            "type": "DWRR",
            "weight": "22"
        },
        "scheduler_q4_downlink": {
            "type": "DWRR",
            "weight": "22"
        },
        "scheduler_q5_downlink": {
            "type": "DWRR",
            "weight": "22"
        },
        "scheduler_q6_downlink": {
            "type": "DWRR",
            "weight": "4"
        },
        "scheduler_q0_uplink": {
            "type": "DWRR",
            "weight": "4"
        },
        "scheduler_q1_uplink": {
            "type": "DWRR",
            "weight": "8"
        },
        "scheduler_q2_uplink": {
            "type": "DWRR",
            "weight": "18"
        },
        "scheduler_q3_uplink": {
            "type": "DWRR",
            "weight": "22"
        },
        "scheduler_q4_uplink": {
            "type": "DWRR",
            "weight": "22"
        },
        "scheduler_q5_uplink": {
            "type": "DWRR",
            "weight": "22"
        },
        "scheduler_q6_uplink": {
            "type": "DWRR",
            "weight": "4"
        }
    },
{%- endmacro -%}

{%- macro generate_direction_based_queue_per_sku(port, direction) -%}
    "{{ port }}|0": {
        "scheduler": "scheduler_q0_{{ direction }}"
    },
    "{{ port }}|1": {
        "scheduler": "scheduler_q1_{{ direction }}",
        "wred_profile": "AZURE_LOSSY_{{ direction.upper() }}_Q1"
    },
    "{{ port }}|2": {
        "scheduler": "scheduler_q2_{{ direction }}",
        "wred_profile": "AZURE_LOSSY_{{ direction.upper() }}_Q2"
    },
    "{{ port }}|3": {
        "scheduler": "scheduler_q3_{{ direction }}",
        "wred_profile": "AZURE_LOSSY_{{ direction.upper() }}_Q3"
    },
    "{{ port }}|4": {
        "scheduler": "scheduler_q4_{{ direction }}",
        "wred_profile": "AZURE_LOSSY_{{ direction.upper() }}_Q4"
    },
    "{{ port }}|5": {
        "scheduler": "scheduler_q5_{{ direction }}",
        "wred_profile": "AZURE_LOSSY_{{ direction.upper() }}_Q5"
    },
    "{{ port }}|6": {
        "scheduler": "scheduler_q6_{{ direction }}"
    }
{%- endmacro -%}

{%- macro generate_global_dscp_to_tc_map() %}
{# This is an empty macro since the global DSCP_TO_TC map is not required #}
{%- endmacro %}

{%- macro generate_wred_profiles() %}
    {# Default threshold values for both ToR and Leaf routers #}
    {%- set min_threshold = "166912" %}
    {%- set disable_wred = traffic_config.wred_threshold_limit %}

    {%- set router_type = (
        DEVICE_METADATA['localhost']['type']
        if 'type' in DEVICE_METADATA['localhost'] and DEVICE_METADATA['localhost']['type']
        else 'ToRRouter'
    ) %}

    {%- if 'ToRRouter' in router_type %}
        {# ToR Router thresholds - only override if different from defaults #}
        {%- set max_threshold = "282624" %}
    {%- elif 'LeafRouter' in router_type %}
        {# Leaf Router thresholds - only override if different from defaults #}
        {%- set max_threshold = "239616" %}
    {%- endif %}

    "WRED_PROFILE": {
        "AZURE_LOSSY_DOWNLINK_Q1": {
            "wred_green_enable": "false",
            "wred_yellow_enable": "false",
            "wred_red_enable": "false",
            "ecn": "ecn_all",
            "green_max_threshold": "{{ max_threshold }}",
            "green_min_threshold": "{{ min_threshold }}",
            "yellow_max_threshold": "{{ max_threshold }}",
            "yellow_min_threshold": "{{ min_threshold }}",
            "red_max_threshold": "{{ max_threshold }}",
            "red_min_threshold": "{{ min_threshold }}",
            "green_drop_probability": "5",
            "yellow_drop_probability": "5",
            "red_drop_probability": "5"
        },
        "AZURE_LOSSY_DOWNLINK_Q2": {
            "wred_green_enable": "false",
            "wred_yellow_enable": "false",
            "wred_red_enable": "false",
            "ecn": "ecn_all",
            "green_max_threshold": "{{ max_threshold }}",
            "green_min_threshold": "{{ min_threshold }}",
            "yellow_max_threshold": "{{ max_threshold }}",
            "yellow_min_threshold": "{{ min_threshold }}",
            "red_max_threshold": "{{ max_threshold }}",
            "red_min_threshold": "{{ min_threshold }}",
            "green_drop_probability": "5",
            "yellow_drop_probability": "5",
            "red_drop_probability": "5"
        },
        "AZURE_LOSSY_DOWNLINK_Q3": {
            "wred_green_enable": "false",
            "wred_yellow_enable": "false",
            "wred_red_enable": "false",
            "ecn": "ecn_all",
            "green_max_threshold": "{{ max_threshold }}",
            "green_min_threshold": "{{ min_threshold }}",
            "yellow_max_threshold": "{{ max_threshold }}",
            "yellow_min_threshold": "{{ min_threshold }}",
            "red_max_threshold": "{{ max_threshold }}",
            "red_min_threshold": "{{ min_threshold }}",
            "green_drop_probability": "5",
            "yellow_drop_probability": "5",
            "red_drop_probability": "5"
        },
        "AZURE_LOSSY_DOWNLINK_Q4": {
            "wred_green_enable": "false",
            "wred_yellow_enable": "false",
            "wred_red_enable": "false",
            "ecn": "ecn_all",
            "green_max_threshold": "{{ disable_wred }}",
            "green_min_threshold": "{{ disable_wred }}",
            "yellow_max_threshold": "{{ disable_wred }}",
            "yellow_min_threshold": "{{ disable_wred }}",
            "red_max_threshold": "{{ disable_wred }}",
            "red_min_threshold": "{{ disable_wred }}",
            "green_drop_probability": "5",
            "yellow_drop_probability": "5",
            "red_drop_probability": "5"
        },
        "AZURE_LOSSY_DOWNLINK_Q5": {
            "wred_green_enable": "false",
            "wred_yellow_enable": "false",
            "wred_red_enable": "false",
            "ecn": "ecn_all",
            "green_max_threshold": "{{ disable_wred }}",
            "green_min_threshold": "{{ disable_wred }}",
            "yellow_max_threshold": "{{ disable_wred }}",
            "yellow_min_threshold": "{{ disable_wred }}",
            "red_max_threshold": "{{ disable_wred }}",
            "red_min_threshold": "{{ disable_wred }}",
            "green_drop_probability": "5",
            "yellow_drop_probability": "5",
            "red_drop_probability": "5"
        },
        "AZURE_LOSSY_UPLINK_Q1": {
            "wred_green_enable": "false",
            "wred_yellow_enable": "false",
            "wred_red_enable": "false",
            "ecn": "ecn_all",
            "green_max_threshold": "{{ max_threshold }}",
            "green_min_threshold": "{{ min_threshold }}",
            "yellow_max_threshold": "{{ max_threshold }}",
            "yellow_min_threshold": "{{ min_threshold }}",
            "red_max_threshold": "{{ max_threshold }}",
            "red_min_threshold": "{{ min_threshold }}",
            "green_drop_probability": "5",
            "yellow_drop_probability": "5",
            "red_drop_probability": "5"
        },
        "AZURE_LOSSY_UPLINK_Q2": {
            "wred_green_enable": "false",
            "wred_yellow_enable": "false",
            "wred_red_enable": "false",
            "ecn": "ecn_all",
            "green_max_threshold": "{{ max_threshold }}",
            "green_min_threshold": "{{ min_threshold }}",
            "yellow_max_threshold": "{{ max_threshold }}",
            "yellow_min_threshold": "{{ min_threshold }}",
            "red_max_threshold": "{{ max_threshold }}",
            "red_min_threshold": "{{ min_threshold }}",
            "green_drop_probability": "5",
            "yellow_drop_probability": "5",
            "red_drop_probability": "5"
        },
        "AZURE_LOSSY_UPLINK_Q3": {
            "wred_green_enable": "false",
            "wred_yellow_enable": "false",
            "wred_red_enable": "false",
            "ecn": "ecn_all",
            "green_max_threshold": "{{ max_threshold }}",
            "green_min_threshold": "{{ min_threshold }}",
            "yellow_max_threshold": "{{ max_threshold }}",
            "yellow_min_threshold": "{{ min_threshold }}",
            "red_max_threshold": "{{ max_threshold }}",
            "red_min_threshold": "{{ min_threshold }}",
            "green_drop_probability": "5",
            "yellow_drop_probability": "5",
            "red_drop_probability": "5"
        },
        "AZURE_LOSSY_UPLINK_Q4": {
            "wred_green_enable": "false",
            "wred_yellow_enable": "false",
            "wred_red_enable": "false",
            "ecn": "ecn_all",
            "green_max_threshold": "{{ disable_wred }}",
            "green_min_threshold": "{{ disable_wred }}",
            "yellow_max_threshold": "{{ disable_wred }}",
            "yellow_min_threshold": "{{ disable_wred }}",
            "red_max_threshold": "{{ disable_wred }}",
            "red_min_threshold": "{{ disable_wred }}",
            "green_drop_probability": "5",
            "yellow_drop_probability": "5",
            "red_drop_probability": "5"
        },
        "AZURE_LOSSY_UPLINK_Q5": {
            "wred_green_enable": "false",
            "wred_yellow_enable": "false",
            "wred_red_enable": "false",
            "ecn": "ecn_all",
            "green_max_threshold": "{{ disable_wred }}",
            "green_min_threshold": "{{ disable_wred }}",
            "yellow_max_threshold": "{{ disable_wred }}",
            "yellow_min_threshold": "{{ disable_wred }}",
            "red_max_threshold": "{{ disable_wred }}",
            "red_min_threshold": "{{ disable_wred }}",
            "green_drop_probability": "5",
            "yellow_drop_probability": "5",
            "red_drop_probability": "5"
        }
    },
{%- endmacro %}

{%- macro generate_lossless_tc_list(lossless_tc_list) %}
{#
    This macro intentionally does nothing, leaving LOSSLESS_TC as an empty list
    Use the following to generate other lists like 3, 4
    {%- if lossless_tc_list.append('3') -%}{%- endif -%}
    {%- if lossless_tc_list.append('4') -%}{%- endif -%}
#}
{%- endmacro %}

{%- macro generate_tc_to_queue_map_per_sku() -%}
    "TC_TO_QUEUE_MAP": {
        "AZURE": {
            "0": "0",
            "1": "1",
            "2": "2",
            "3": "3",
            "4": "4",
            "5": "5",
            "6": "6",
            "7": "7"
{%- if traffic_config.traffic_classification_enable %}
            ,
            "8": "4"
{%- endif %}
        }
    },
{%- endmacro -%}

{% macro generate_tc_to_dscp_map_per_sku() %}
{%- if traffic_config.traffic_classification_enable %}
    "TC_TO_DSCP_MAP": {
        {% if 'type' in DEVICE_METADATA['localhost'] %}
            {%- if 'ToRRouter' in DEVICE_METADATA['localhost']['type'] -%}
                "AZURE_DOWNLINK_BT0": {
                    "8": "21"
                },
                "AZURE_UPLINK_BT0": {
                    "8": "11"
                }
            {%- elif 'LeafRouter' in DEVICE_METADATA['localhost']['type'] -%}
                "AZURE_DOWNLINK_BT1": {
                    "8": "11"
                }
            {% endif %}
        {% endif -%}
    },
{%- endif %}
{%- endmacro -%}

{%- macro generate_port_qos_map_per_sku(ports, direction) -%}
{%- for port in ports.split(',') %}
        "{{ port }}": {
            "dscp_to_tc_map"  : "AZURE",
            "tc_to_queue_map" : "AZURE",
            "pfc_enable"      : "",
            "pfcwd_sw_enable" : "",
            {% if traffic_config.traffic_classification_enable %}
                {% if direction == 'downlink' and 'ToRRouter' in DEVICE_METADATA['localhost']['type'] %}
                    "tc_to_pg_map": "AZURE",
                    "tc_to_dscp_map": "AZURE_DOWNLINK_BT0"
                {% elif direction == 'uplink' and 'ToRRouter' in DEVICE_METADATA['localhost']['type'] %}
                    "tc_to_pg_map": "AZURE",
                    "tc_to_dscp_map": "AZURE_UPLINK_BT0"
                {% elif direction == 'downlink' and 'LeafRouter' in DEVICE_METADATA['localhost']['type'] %}
                    "tc_to_pg_map": "AZURE",
                    "tc_to_dscp_map": "AZURE_DOWNLINK_BT1"
                {% else %}
                    "tc_to_pg_map": "AZURE"
                {% endif %}
            {% else %}
                "tc_to_pg_map": "AZURE"
            {% endif %}
        }{% if not loop.last %},{% endif %}
{%- endfor %}
{%- endmacro -%}

{%- include 'qos_config.j2' %}
